package com.alan.repository.impl;

import com.alan.entity.student;
import com.alan.entity.teacherstudent;
import com.alan.repository.StudentRepository;
import com.alan.utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentRepositoryImpl implements StudentRepository {
    @Override
    public student login(String username, String password) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        student student = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from studentlogin where name=? and password=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                student = new student(resultSet.getString(1), resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return student;

    }

    @Override
    public teacherstudent find(String username) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        teacherstudent teacherstudent = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from student where student_id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                teacherstudent = new teacherstudent(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), resultSet.getInt(6), resultSet.getDouble(7), resultSet.getDouble(8), resultSet.getString(9));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return teacherstudent;
    }

    @Override
    public void update(String stu_id, String Cname, String Ename, String Ntel) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "update  student set chinese_name=?,english_name=?,tel=? where student_id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, Cname);
            preparedStatement.setString(2, Ename);
            preparedStatement.setString(3, Ntel);
            preparedStatement.setString(4, stu_id);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }

    @Override
    public void change(String name, String Npassword) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "update  studentlogin set password=? where name=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, Npassword);
            preparedStatement.setString(2, name);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }

    @Override
    public void changemarks(String name, double mid, double end) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "update  student set mid_marks=?,end_marks=? where student_id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setDouble(1, mid);
            preparedStatement.setDouble(2, end);
            preparedStatement.setString(3, name);
            int a = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }

    @Override
    public void addstudent(String name, String tclass) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "insert into student set student_id=?,stu_class=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, tclass);
            int a = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
    }

    @Override
    public void addlogin(String name, String password) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "insert  into studentlogin values (?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, password);
            int a = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }

    }

    @Override
    public teacherstudent selfmarks(String name) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        teacherstudent teacherstudent = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from student where student_id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                teacherstudent = new teacherstudent(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), resultSet.getInt(6), resultSet.getDouble(7), resultSet.getDouble(8), resultSet.getString(9));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return teacherstudent;
    }


}


